// PalmMsg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "MsgVolParser.h"
#include "PalmMsg.h"

const char *FILE_MSG_DB = "PalmMsgV001";
const char *FILE_INBOX  = "Export_Received.csv";
const char *FILE_OUTBOX = "Export_Sent.csv";

const BYTE utf8_BOM[4] = {0xEF,0xBB,0xBF,0x00};

FILE *fpOut[MAX_MSGTYPES];

void palmmsg_user_prompt(void) {
	printf("\n\n");
	printf("\tSMS Extractor for Treo 750 (v.0.2)\n\nUsage:\n");
	printf("\t1. Run this tool with your PalmMsgV001 in same folder.\n");
	printf("\t   It will try to export as many SMS as it can.\n\n");
	printf("\t2. There's no garantee from author.\n");
	printf("\t   You're using this software on your own risk.\n\n");
	printf("\t3. Version 0.2 is few buggy than 0.1 and will expire at 2009.12.31:\n");
	printf("\t   1> Export only INBOX & SENT SMS.\n");
	printf("\t   2> Cannot read the SMS time stamp\n");
	printf("\t   3> Rarely memory read / write error.\n\n");
	printf("\t But! NOBODY DO THE SAME JOB BETTER THAN IT. 2009.4.14\n");
	printf("\t Please contact author at www.treo.net.cn - Labyrinth\n");
	printf("\t I've spend now 30h+ writing it and offer it for free to who loves TREO750.\n");
	printf("\n");
}
//
//	Prompt and wait for any key to continue:
//	In:	Prompt string
void palmmsg_pause_prompt(const char * prompt) {
	printf(prompt);
	_getch();
}

//
//	Check whether program expires
//	In:	None
//	Re:	 0 - Expired
//		>0 - Valid
int palmmsg_checkexpire(void) {
	int rc = 0;
	time_t today;
	time(&today);
	rc = ( today > EXPIRE_AT_091231 ? PALMMSG_EXPIRED : PALMMSG_NOT_EXPIRED );
	if ( rc == PALMMSG_EXPIRED ) {
		printf ("!!! This buggy version is expired.\n");
		printf ("!!! Please visit www.treo.net.cn for most recent update.\n\n");
	}
	return rc;
}
//
//	Program initialization
//	In:	None
//	Re:	 0 - Expired
// 		>0 - Valid
int palmmsg_initialization (void) {
	int rc = 0;
	palmmsg_user_prompt();
	rc = palmmsg_checkexpire();
	palmmsg_pause_prompt("\tHit any key to continue...\n\n");
	return rc;
};
//
//	Prepare file to output
//	In:	None
//	Re:	 0 - Open file error
//		>0 - Success
int palmmsg_prepare_outputs(void) {
	int rc = 0, i;
	errno_t err[MAX_MSGTYPES];
	for (i=0;i<MAX_MSGTYPES;i++) 
		fpOut[i] = NULL;
	err[1] = fopen_s(&fpOut[1], FILE_INBOX,"wt");
	err[3] = fopen_s(&fpOut[3], FILE_OUTBOX,"wt");
	if ((err[1]==0) && (err[3]==0)) {
		fwrite(utf8_BOM,1,3,fpOut[1]);
		fwrite(utf8_BOM,1,3,fpOut[3]);
		rc = 1 + 1;
	} else {
#ifdef PM_DEBUG
		printf("Unable to open file for export.\n");
#endif
	}
	return rc;
}

void palmmsg_deinit_outputs(void) {
	int i;
	for (i=0; i<MAX_MSGTYPES; i++) {
		if(fpOut[i]!=NULL) 
			fclose(fpOut[i]);
	}
}

int main(int argc, char* argv[]) {

	s_SMS_t sSMS;

	if (palmmsg_initialization()==PALMMSG_EXPIRED) 
		return -1;	// Program expires.
	
	if (msgvolparser_init(FILE_MSG_DB)==RC_ERROR)
		return -2;	// Prepare input error.

	if (palmmsg_prepare_outputs()==RC_ERROR)
		return -3;	// Prepare output error.

	while ( msgvolparser_locate_next_SMS_Header() > 0 ) {
		if ( msgvolparser_analyse_sms_packet (&sSMS) > 0 ) {
			if (( sSMS.type_SMS == 1 ) || ( sSMS.type_SMS == 3 )) {
				msgbody_export(&sSMS, fpOut[sSMS.type_SMS]);
			} else if (sSMS.type_SMS == 6 ) {
				msgbody_export(&sSMS, fpOut[1]);
			}
		}
	}

	msgvolparser_deinit();
	palmmsg_deinit_outputs();

	msgvolparser_print_read_results();

	palmmsg_pause_prompt ("\n\tExport completed!\n\n");

	return 0;
}